package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.MarketingClassifyBrandDAO;
import com.ecoolex.discount.sales.entity.DiscountBrandClassify;
import com.ecoolex.discount.sales.entity.MarketingClassifyBrand;
import com.ecoolex.discount.sales.entity.MarketingClassifyBrand.Column;
import com.ecoolex.discount.sales.entity.MarketingClassifyBrandExample;
import com.ecoolex.discount.sales.entity.MarketingClassifyBrandExample.Criteria;
import com.ecoolex.discount.sales.pojo.QueryMarketingClassifyBrandDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.exception.BizException;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MarketingClassifyBrandManager {
	
    @Autowired
    private MarketingClassifyBrandDAO dao;
    
    @Autowired
    private DiscountBrandClassifyManager discountBrandClassifyManager;
    
    public void verify(MarketingClassifyBrand record) {
    	BizAssert.isTrue(Check.notNullOrEmpty(record.getPosition()),ResultCode.VERIFY.build(10), "位置信息为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getClassifyId()),ResultCode.VERIFY.build(11), "分类id为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getBrandId()),ResultCode.VERIFY.build(12), "品牌id为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getRegionType()),ResultCode.VERIFY.build(13), "区域类型为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getWeight()),ResultCode.VERIFY.build(14), "权重为空为空");
    	DiscountBrandClassify brandClassify = discountBrandClassifyManager.getByClassifyIdAndBrandId(record.getClassifyId(), record.getBrandId());
    	BizAssert.isTrue(Check.notNull(brandClassify),ResultCode.VERIFY.build(15), "品牌id={},不属于分类id={}",record.getBrandId(),record.getClassifyId());
    }
    
    public MarketingClassifyBrand addMarketingClassifyBrand(MarketingClassifyBrand record, String createUser) throws BizException {
    	verify(record);
    	List<MarketingClassifyBrand> list = queryByPosition(record.getRegionType(),record.getPosition(),record.getClassifyId(),record.getBrandId());
    	BizAssert.isTrue(Check.isNullOrEmpty(list),ResultCode.VERIFY.build(16),"分类下品牌营销重复");
    	record.setDeleted(MarketingClassifyBrand.NOT_DELETED);
    	record.setCreateUser(createUser);
    	record.setCreateTime(Date.from(Instant.now()));
    	record.setUpdateUser(createUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.insert(record);
    	return record;
	}

	public MarketingClassifyBrand editMarketingClassifyBrand(MarketingClassifyBrand record, String updateUser) throws BizException {
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.weight,Column.updateUser,Column.updateTime);
		return record;
	}

	public void deleteMarketingClassifyBrand(MarketingClassifyBrand record, String updateUser) throws BizException {
		record.setDeleted(MarketingClassifyBrand.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
	}

	public MarketingClassifyBrand getMarketingClassifyBrand(Integer id) {
		return dao.selectByPrimaryKeyWithLogicalDelete(id, MarketingClassifyBrand.NOT_DELETED);
	}

	public Pagination<MarketingClassifyBrand> queryMarketingClassifyBrand(QueryMarketingClassifyBrandDTO query,Sorting[] sorts, Page page) {
		MarketingClassifyBrandExample example = new MarketingClassifyBrandExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNull(query.getRegionType())) {
			criteria.andRegionTypeEqualTo(query.getRegionType());
		}
		if(Check.notNullOrTrimEmpty(query.getPosition())) {
			criteria.andPositionEqualTo(query.getPosition());
		}
		if(Check.notNull(query.getClassifyId())) {
			criteria.andClassifyIdEqualTo(query.getClassifyId());
		}
		criteria.andDeletedEqualTo(MarketingClassifyBrand.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public Pagination<MarketingClassifyBrand> queryMarketingClassifyBrandForH5(QueryMarketingClassifyBrandDTO query, Page page){
		return Pagination.create(page, 
			dao.countForH5(query),
			() -> dao.queryForH5(query, page));
	}
	
	public List<MarketingClassifyBrand> queryByPosition(Integer regionType,String position,Integer classifyId,Integer brandId){
		MarketingClassifyBrandExample example = new MarketingClassifyBrandExample();
		Criteria criteria = example.createCriteria();
		criteria.andRegionTypeEqualTo(regionType).andPositionEqualTo(position).andClassifyIdEqualTo(classifyId).andBrandIdEqualTo(brandId).andDeletedEqualTo(MarketingClassifyBrand.NOT_DELETED);
		return dao.selectByExample(example);
	}
	
	public List<MarketingClassifyBrand> queryByBrandId(Integer brandId){
		MarketingClassifyBrandExample example = new MarketingClassifyBrandExample();
		Criteria criteria = example.createCriteria();
		criteria.andBrandIdEqualTo(brandId).andDeletedEqualTo(MarketingClassifyBrand.NOT_DELETED);
		return dao.selectByExample(example);
	}
}